package com.qixin.modules.rzInvoice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qixin.modules.ea.entity.EaBookDetail;
import com.qixin.modules.ea.service.IEaBookDetailService;
import com.qixin.modules.rzInvoice.entity.EaBill;
import com.qixin.modules.rzInvoice.service.IEaBillService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

/**
 * @Description: ea_bill
 * @Author: qixin-boot
 * @Date: 2021-09-10
 * @Version: V1.0
 */
@Api(tags = "ea_bill")
@RestController
@RequestMapping("api/app/eaBill/eaBill")
@Slf4j
public class APIEaBillController extends JeecgController<EaBill, IEaBillService> {
    @Autowired
    private IEaBillService eaBillService;
    @Autowired
    private IEaBookDetailService eaBookDetailService;

    /**
     * 分页列表查询
     *
     * @param eaBill
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "ea_bill-分页列表查询")
    @ApiOperation(value = "ea_bill-分页列表查询", notes = "ea_bill-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(EaBill eaBill,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        QueryWrapper<EaBill> queryWrapper = QueryGenerator.initQueryWrapper(eaBill, req.getParameterMap());
        Page<EaBill> page = new Page<EaBill>(pageNo, pageSize);
        IPage<EaBill> pageList = eaBillService.page(page, queryWrapper);
        return Result.OK(pageList);
    }

    /**
     * 添加
     *
     * @param eaBill
     * @return
     */
    @AutoLog(value = "ea_bill-添加")
    @ApiOperation(value = "ea_bill-添加", notes = "ea_bill-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody EaBill eaBill) {
        eaBillService.save(eaBill);
        return Result.OK("添加成功！");
    }

    /**
     * 编辑
     *
     * @param eaBill
     * @return
     */
    @AutoLog(value = "ea_bill-编辑")
    @ApiOperation(value = "ea_bill-编辑", notes = "ea_bill-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody EaBill eaBill) {
        eaBillService.updateById(eaBill);
        return Result.OK("编辑成功!");
    }

    /**
     * 通过id删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ea_bill-通过id删除")
    @ApiOperation(value = "ea_bill-通过id删除", notes = "ea_bill-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        eaBillService.removeById(id);
        return Result.OK("删除成功!");
    }

    /**
     * 批量删除
     *
     * @param ids
     * @return
     */
    @AutoLog(value = "ea_bill-批量删除")
    @ApiOperation(value = "ea_bill-批量删除", notes = "ea_bill-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        this.eaBillService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "ea_bill-通过id查询")
    @ApiOperation(value = "ea_bill-通过id查询", notes = "ea_bill-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        EaBill eaBill = eaBillService.getById(id);
        if (eaBill == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(eaBill);
    }

    /**
     * 发票更新
     *
     * @param billIds
     * @return
     */
    @AutoLog(value = "发票更新")
    @ApiOperation(value = "发票更新", notes = "发票更新")
    @PutMapping(value = "/updeteFpState")
    @Transactional
    public Result<?> updeteFpState(@RequestParam(name = "billIds", required = true) String billIds) {
        if (!StringUtils.isEmpty(billIds)) {
            String[] ids = billIds.split(",");
            for (String id : ids) {
                EaBookDetail eaBookDetail = eaBookDetailService.selectById(id);
                EaBill eaBill = eaBillService.getById(eaBookDetail.getFpid());
                // 发票被引用
                if ("1".equals(eaBill.getIsused()) && eaBookDetail.getBillId() != null) {
                    // 关联的表单id置为空
                    eaBookDetail.setBillId("");
                    // 取消费用明细行和业务表单的关系
                    eaBillService.updateEabill(eaBookDetail);
                    // 发票状态更新
                    eaBill.setIsused("0");
                    eaBillService.updateById(eaBill);
                }
            }
        }
        return Result.OK("发票更新成功");
    }

    /**
     * 导出excel
     *
     * @param request
     * @param eaBill
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, EaBill eaBill) {
        return super.exportXls(request, eaBill, EaBill.class, "ea_bill");
    }

    /**
     * 通过excel导入数据
     *
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, EaBill.class);
    }

}
